package com.ruoyi.project.yuwen.topman.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.yuwen.topman.domain.AudioTopman;
import com.ruoyi.project.yuwen.topman.service.IAudioTopmanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 朗讼达人Controller
 *
 * @author shixiangwen
 * @date 2021-03-09
 */
@Controller
@RequestMapping("/yuwen/topman")
public class AudioTopmanController extends BaseController {
    private String prefix = "yuwen/topman";

    @Autowired
    private IAudioTopmanService audioTopmanService;

    @RequiresPermissions("yuwen:topman:view")
    @GetMapping()
    public String topman() {
        return prefix + "/topman";
    }

    /**
     * 查询朗讼达人列表
     */
    @RequiresPermissions("yuwen:topman:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AudioTopman audioTopman) {
        startPage();
        List<AudioTopman> list = audioTopmanService.selectAudioTopmanList(audioTopman);
        return getDataTable(list);
    }

    /**
     * 导出朗讼达人列表
     */
    @RequiresPermissions("yuwen:topman:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AudioTopman audioTopman) {
        List<AudioTopman> list = audioTopmanService.selectAudioTopmanList(audioTopman);
        ExcelUtil<AudioTopman> util = new ExcelUtil<AudioTopman>(AudioTopman.class);
        return util.exportExcel(list, "topman");
    }

    /**
     * 新增朗讼达人
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存朗讼达人
     */
    @RequiresPermissions("yuwen:topman:add")
    @Log(title = "朗讼达人", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AudioTopman audioTopman) {
        return toAjax(audioTopmanService.insertAudioTopman(audioTopman));
    }

    /**
     * 修改朗讼达人
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AudioTopman audioTopman = audioTopmanService.selectAudioTopmanById(id);
        mmap.put("audioTopman", audioTopman);
        return prefix + "/edit";
    }

    /**
     * 修改保存朗讼达人
     */
    @RequiresPermissions("yuwen:topman:edit")
    @Log(title = "朗讼达人", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AudioTopman audioTopman) {

        AudioTopman old = audioTopmanService.selectAudioTopmanById(audioTopman.getId());
        int upType = 0;
        String remark = "";
        if (audioTopman.getLevel() != old.getLevel()) {
            upType = 2;
            remark = ShiroUtils.getLoginName() + "手动调级[" + audioTopman.getRemark() + "]";
        }

        int effect = audioTopmanService.updateAudioTopman(audioTopman, upType, remark);

        return toAjax(effect);
    }

    @RequiresPermissions("yuwen:topman:edit")
    @Log(title = "朗讼达人", businessType = BusinessType.UPDATE)
    @PostMapping("/approveUpLevel")
    @ResponseBody
    public AjaxResult approveUpLevel(Long id) {
        AudioTopman audioTopman = audioTopmanService.selectAudioTopmanById(id);
        if (audioTopman.getScoreLevel() > audioTopman.getLevel()) {
            audioTopman.setLevel(audioTopman.getScoreLevel());
        }
        String remark = "作品达到要求," + ShiroUtils.getLoginName() + "审核通过";
        return toAjax(audioTopmanService.updateAudioTopman(audioTopman, 1, remark));
    }


    /**
     * 删除朗讼达人
     */
    @RequiresPermissions("yuwen:topman:remove")
    @Log(title = "朗讼达人", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(audioTopmanService.deleteAudioTopmanByIds(ids));
    }
}
